package com.hujh.android;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;

public class HandlerActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_handler);
		
		//handler与activity在不的线程中
		//普通的handler.post(thread)只是同一线程中，并没有调用thread的start()方法
		HandlerThread handlerThead = new HandlerThread("looperThead");
		handlerThead.start();
		MyHandler myHandler = new MyHandler(handlerThead.getLooper());
		Message msg = myHandler.obtainMessage();
		msg.arg1 = 1;
		msg.sendToTarget(); //发送到handler
		
		System.out.println("activity:" + Thread.currentThread().getId());
		
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	
	class MyHandler extends Handler{
		
		public MyHandler(){};
		
		public MyHandler(Looper looper){
			super(looper);
		}

		@Override
		//处理消息
		public void handleMessage(Message msg) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("handler:" + Thread.currentThread().getId());
			System.out.println("handler:" + Thread.currentThread().getName());
			System.out.println(msg.arg1);
			
		}
		
		
		
	}
		
}
